﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Brain;

namespace AvaProvi
{
    public partial class ctlEMTerreno : AvaProvi.ControlBase
    {
        VSimbolo vs;
        Validadora val;

        public ctlEMTerreno()
            : base()
        {
            InitializeComponent();
            ActualizaPanel();

        }

        public override void ActualizaDatos()
        {
            base.ActualizaDatos();
            this.bsEnfoqueDeMercado.EndEdit();
            clsAvaluo_CRN.AvaluoActual.Guardar();
            ActualizaPanel();
        }

        public override void ActualizaPanel()
        {
            this.bsEnfoqueDeMercado.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado;
            this.bsEnfoqueDeMercado.ResetBindings(false);
            this.bsConstruccion.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.Construccion;
            this.bsConstruccion.ResetBindings(false);
            this.baseUltraGroupBox.Text = "Enforque de Mercado:Terreno(De " + clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ValorDeTerrenoPermitidoDesde.ToString() + "m² a " + clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ValorDeTerrenoPermitidoHasta.ToString() + "m²)";
        }

        private void gvFactores_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.bsEnfoqueDeMercado.ResetBindings(false);
            this.bsConstruccion.ResetBindings(false);
        }

        private void btnCopiar_Click(object sender, EventArgs e)
        {
            if (bsEnfoqueTerreno.Current != null)
            {
                clsEnfoqueMercadoTerreno_CRN objEnfoqueOriginal = (clsEnfoqueMercadoTerreno_CRN)(bsEnfoqueTerreno.Current);
                clsEnfoqueDeMercado_CRN.CopiaDeEnfoque = objEnfoqueOriginal.Clone();
            }
        }

        private void btnPegar_Click(object sender, EventArgs e)
        {
            if (bsEnfoqueTerreno.Current != null)
            {
                clsEnfoqueMercadoTerreno_CRN objEnfoqueOriginal = (clsEnfoqueMercadoTerreno_CRN)(bsEnfoqueTerreno.Current);
                objEnfoqueOriginal.Pegar(clsEnfoqueDeMercado_CRN.CopiaDeEnfoque);
                this.bsEnfoqueDeMercado.ResetBindings(false);
            }
        }

        private void superficieTotalDeConstruccionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
             vs = new VSimbolo();
             e.Handled = vs.esDecimal(e.KeyChar, (superficieTotalDeConstruccionTextBox.Text).Trim());
        }

        private void txtValorHomologado_KeyPress(object sender, KeyPressEventArgs e)
        {
             vs = new VSimbolo();
             e.Handled = vs.esDecimal(e.KeyChar, (txtValorHomologado.Text).Trim());
        }

        private void valorComparativoDeMercadoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
             vs = new VSimbolo();
             e.Handled = vs.esDecimal(e.KeyChar, (valorComparativoDeMercadoTextBox.Text).Trim());
        }

        private void gvFactores_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gFac; 
        }
        private void validar_Keypress_gFac(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int col = gvFactores.CurrentCell.ColumnIndex;
            if (col == 1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7 || col == 8 || col == 9 || col == 10)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }
        private void gvFactores_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvFactores.Columns[e.ColumnIndex].Index;
            if (gv == 1 || gv == 2 || gv == 3 || gv == 4 || gv == 5 || gv == 6 || gv == 7 || gv == 8 || gv == 9 || gv == 10)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
            
        }

        private void baseUltraGroupBox_Click(object sender, EventArgs e)
        {

        }

        private void gvOtrosDatos_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}
